package com.kystar.muxplayer.manager;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ScrollView;
import android.widget.ToggleButton;

import com.kystar.muxplayer.BaseApp;
import com.kystar.muxplayer.Helper;
import com.kystar.muxplayer.PresetAdapter;
import com.kystar.muxplayer.R;
import com.kystar.muxplayer.protocol.code.set.CS13_Preset;
import com.kystar.muxplayer.selfview.PvmFrameLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by peanut on 2018/8/21
 */
public class ThreeBottomManager {
    ToggleButton lockLocation;
    ToggleButton layerInfo;
    View saveSet;
    PvmFrameLayout mFrameLayout;
    static  ThreeBottomManager sThreeBottomManager;
    private ThreeBottomManager(){

    }

    public static ThreeBottomManager getInstance(){
        if(sThreeBottomManager == null){
            sThreeBottomManager = new ThreeBottomManager();
        }
        return  sThreeBottomManager;
    }

    public void initView(ToggleButton lockLocation,ToggleButton layerInfo,View saveSet){
        this.lockLocation = lockLocation;
        this.layerInfo = layerInfo;
        this.saveSet = saveSet;
        mFrameLayout = PvmManager.getPvmSingleInstance().getFrameLayout();
        lockLocation.setChecked(false);
        lockLocation.setOnCheckedChangeListener((v, isChecked) -> {
            mFrameLayout.setLockLocation(isChecked);
        });
        layerInfo.setChecked(true);
        layerInfo.setOnCheckedChangeListener((v, isChecked) -> {
            mFrameLayout.setShowInfo(isChecked);
        });
        saveSet.setOnClickListener(v -> {
            LinearLayout listView = new LinearLayout(mFrameLayout.getContext());
            listView.setOrientation(LinearLayout.VERTICAL);
            listView.setBackgroundResource(R.color.spinner_dropdown_bg);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    saveSet.getMeasuredWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);

            List<View> items = new ArrayList<>();
            final PresetAdapter adapter = new PresetAdapter(mFrameLayout.getContext(), Arrays.asList(BaseApp.getSettings().getPresets()));
            for (int i=0; i<adapter.getCount(); i++) {
                View view = adapter.getView(i, null, null);
                view.setTag(i+1);
                view.setPadding(0,7,0,7);
                items.add(view);
                listView.addView(view, params);
            }
            Helper.hideSystemUI(listView);
            ScrollView scrollView = new ScrollView(mFrameLayout.getContext());
            scrollView.addView(listView);
            PopupWindow popup = Helper.showDropDownWindow(saveSet, scrollView, true, null);
            Helper.each(items, view->{
                view.setOnClickListener(v1->{
                    popup.dismiss();
                    int presetId  = Helper.getTagInt(v1,0);
                    BaseApp.send(CS13_Preset.createSave(presetId)).subscribe(cmd->{
                        BaseApp.getSettings().getPresets()[presetId-1].setSet(true);
                        PresetLayoutManager.getPresetSingleInstance().mAdapter.notifyDataSetChanged();
                    });
                });
            });
        });
    }
}
